/* exec.c
 *      Test program for the exec statement in nachos
 */
#include "syscall.h"

int main() {

    char fname[] = "sharedmem2";
    char arg[] = "1";
    char* args[2] = {arg, 0};

    int status;
    int child_id;

    if((void*)args == (void*)arg) {
        Exit(3);
    }

    child_id = Exec(fname, args);

    int memid = Shmget(1,20,SHM_USE);
    char *mem = Shmat(memid);
    mem[0] = 'a';
    mem[1] = 't';
    mem[2] = 's';
    mem[4] = '\n';
    Write("mem contains:\n\t", 15, ConsoleOutput);
    Write(mem, 5, ConsoleOutput);
    
    //Shmdt(mem); 

    //if the exec call failed, then return an exit code to be able to tell
    //it failed
    if(child_id == -1) {
        Exit(2);
    }

    status = Join(child_id);
    if(status == 0) {
        //we had a success
        Write("yup\n",4,ConsoleOutput);
        Exit(0);
    } else {
        Write("nope\n",5,ConsoleOutput);
        Exit(status);
    }

}
